﻿Imports System.Timers
Public Class MainSearchForm

    Public apotelesmata As ResultsTable = New ResultsTable
    Public pinakasapotelesmaton As ResultsItem()
    Dim pinakasResultsRow(0 To 9) As ResultsRow
    Private page As Integer = 0
    Public flag1 As Boolean = False
    Public flag2 As Boolean = False
    Public flag3 As Boolean = False
    Dim RowIndex As Integer
    Dim ResultsPanel As Panel
    Private Shared WithEvents myTimer As New System.Windows.Forms.Timer()

    Public Sub myInit()

        For i As Integer = 0 To 9 'pinakasapotelesmaton.Length - 1
            If Not (pinakasResultsRow(i) Is Nothing) Then
                pinakasResultsRow(i).Parent = Nothing

                pinakasResultsRow(i) = Nothing
            End If
        Next
        If Not ResultsPanel Is Nothing Then

            ResultsPanel.Parent = Nothing
        End If

        ResultsPanel = Nothing

        ProgressBar1.Value = 0
    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click

        myInit()

        ResultsPanel = New Panel
        ResultsPanel.Parent = Me
        ResultsPanel.Location = New System.Drawing.Point(66, 135)
        ResultsPanel.Size = New System.Drawing.Size(730, 183)
        ResultsPanel.Height = PagingPanel.Top - 30 - btnSearch.Location.Y * 2 - btnSearch.Height
        ResultsPanel.Top = btnSearch.Location.Y * 2 + btnSearch.Height
        ResultsPanel.AutoScroll = True

        apotelesmata = New ResultsTable
        apotelesmata.reset()
        flag1 = False
        flag2 = False
        flag3 = False

        If txtSearchString.Text = "" Then
            txtSearchString.Text = "You trolling bro?"
            Return
        Else
            HiddenForm1.WebBrowser1.Navigate("https://www.google.gr/search?q=" & txtSearchString.Text)
            'HiddenForm1.Visible = True

            HiddenForm2.WebBrowser1.Navigate("http://www.bing.com/search?q=" & txtSearchString.Text)
            'HiddenForm2.Visible = True

            HiddenForm3.WebBrowser1.Navigate("http://gr.search.yahoo.com/search?p=" & txtSearchString.Text)
            'HiddenForm3.Visible = True

            myTimer.Interval = 15000
            myTimer.Start()
        End If
    End Sub

    Public Sub ReadyToView()

        If flag1 And flag2 And flag3 Then
            myTimer.Stop()
            pinakasapotelesmaton = apotelesmata.sortAndCombine()

            numResults.Text = pinakasapotelesmaton.Length
            'ProgressBar1.Value = 300


            'Dim pinakasResultsRow(pinakasapotelesmaton.Length) As ResultsRow
            For i As Integer = 0 To 9 'pinakasapotelesmaton.Length - 1
                If Not (pinakasapotelesmaton(i) Is Nothing) Then
                    If i = 0 Then
                        pinakasResultsRow(i) = New ResultsRow
                        pinakasResultsRow(i).FillRow(pinakasapotelesmaton(i))
                        pinakasResultsRow(i).Size = New System.Drawing.Size(700, 80)
                        pinakasResultsRow(i).Location = New System.Drawing.Point(0, 0)
                        Me.ResultsPanel.Controls.Add(pinakasResultsRow(i))
                        pinakasResultsRow(i).Parent = Me.ResultsPanel
                        Me.Refresh()
                        Me.Update()
                        ResultsPanel.Refresh()
                        ResultsPanel.Update()

                    Else
                        pinakasResultsRow(i) = New ResultsRow
                        pinakasResultsRow(i).FillRow(pinakasapotelesmaton(i))
                        pinakasResultsRow(i).Size = New System.Drawing.Size(700, 80)
                        pinakasResultsRow(i).Location = New System.Drawing.Point(0, (pinakasResultsRow(i - 1).Location.Y + pinakasResultsRow(i - 1).Height + 20))
                        pinakasResultsRow(i).Parent = Me.ResultsPanel
                        Me.ResultsPanel.Controls.Add(pinakasResultsRow(i))
                        Me.Refresh()
                        Me.Update()
                        ResultsPanel.Refresh()
                        ResultsPanel.Update()
                    End If

                End If
            Next
        End If
    End Sub

    Private Sub PrevPB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrevPB.Click
        If RowIndex > 0 And Not RowIndex = 10 Then
            page = page + 1
        End If
        Try
            'If flag1 Or flag2 Or flag3 Then
            'pinakasResultsRow()
            RowIndex = 0
            page = page - 1
            For i As Integer = page * 10 To page * 10 + 9
                If Not (pinakasapotelesmaton(i) Is Nothing) Then
                    pinakasResultsRow(RowIndex).Visible = True
                    pinakasResultsRow(RowIndex).FillRow(pinakasapotelesmaton(i))
                End If
                Me.Refresh()
                RowIndex = RowIndex + 1
            Next
            'End If
        Catch ex As Exception
            page = page + 1
        End Try
    End Sub

    Private Sub NextPB_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles NextPB.Click
        Try
            'If flag1 Or flag2 Or flag3 Then
            'pinakasResultsRow()
            RowIndex = 0
            page = page + 1
            For i As Integer = page * 10 To page * 10 + 9
                If Not (pinakasapotelesmaton(i) Is Nothing) Then

                    'pinakasResultsRow(RowIndex) = New ResultsRow
                    pinakasResultsRow(RowIndex).FillRow(pinakasapotelesmaton(i))
                End If
                Me.Refresh()
                RowIndex = RowIndex + 1
            Next
            'End If
        Catch ex As Exception
            For i As Integer = RowIndex To 9
                pinakasResultsRow(i).Visible = False
                Me.Refresh()
            Next
            page = page - 1
        End Try
    End Sub

    Private Sub txtSearchString_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtSearchString.KeyPress
        If e.KeyChar = vbCr Then
            Button1_Click(sender, e)
        End If
    End Sub


    Private Shared Sub TimerEventProcessor(ByVal myObject As Object, ByVal myEventArgs As EventArgs) Handles myTimer.Tick
        MainSearchForm.pinakasapotelesmaton = MainSearchForm.apotelesmata.sortAndCombine()

        MainSearchForm.numResults.Text = MainSearchForm.pinakasapotelesmaton.Length
        MainSearchForm.ProgressBar1.Value = 300

        If (MainSearchForm.pinakasapotelesmaton.Length = 0) Then
            MainSearchForm.numResults.Text = "No results found"
            Exit Sub
        End If

        'Dim pinakasResultsRow(pinakasapotelesmaton.Length) As ResultsRow
        For i As Integer = 0 To 9 'pinakasapotelesmaton.Length - 1
            If Not (MainSearchForm.pinakasapotelesmaton(i) Is Nothing) Then
                If i = 0 Then
                    MainSearchForm.pinakasResultsRow(i) = New ResultsRow
                    MainSearchForm.pinakasResultsRow(i).FillRow(MainSearchForm.pinakasapotelesmaton(i))
                    MainSearchForm.pinakasResultsRow(i).Size = New System.Drawing.Size(700, 80)
                    MainSearchForm.pinakasResultsRow(i).Location = New System.Drawing.Point(0, 0)
                    MainSearchForm.ResultsPanel.Controls.Add(MainSearchForm.pinakasResultsRow(i))
                    MainSearchForm.pinakasResultsRow(i).Parent = MainSearchForm.ResultsPanel
                    MainSearchForm.Refresh()
                    MainSearchForm.Update()
                    MainSearchForm.Refresh()
                    MainSearchForm.Update()

                Else
                    MainSearchForm.pinakasResultsRow(i) = New ResultsRow
                    MainSearchForm.pinakasResultsRow(i).FillRow(MainSearchForm.pinakasapotelesmaton(i))
                    MainSearchForm.pinakasResultsRow(i).Size = New System.Drawing.Size(700, 80)
                    MainSearchForm.pinakasResultsRow(i).Location = New System.Drawing.Point(0, (MainSearchForm.pinakasResultsRow(i - 1).Location.Y + MainSearchForm.pinakasResultsRow(i - 1).Height + 20))
                    MainSearchForm.pinakasResultsRow(i).Parent = MainSearchForm.ResultsPanel
                    MainSearchForm.ResultsPanel.Controls.Add(MainSearchForm.pinakasResultsRow(i))
                    MainSearchForm.Refresh()
                    MainSearchForm.Update()
                    MainSearchForm.ResultsPanel.Refresh()
                    MainSearchForm.ResultsPanel.Update()
                End If

            End If
        Next
    End Sub

End Class
